package com.lingjtx.auth.config;

import com.lingjtx.auth.entity.DevUserInfo;
import com.lingjtx.auth.entity.SysUserInfo;
import com.lingjtx.auth.entity.TobUserInfo;
import com.lingjtx.auth.entity.TocUserInfo;
import com.lingjtx.auth.handle.UserServiceContext;
import com.lingjtx.auth.service.UserHandleService;
import jakarta.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@Component
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class UserServiceContextWrapper {

    private final UserServiceContext userServiceContext;

    private final UserHandleService<TocUserInfo> tocUserInfoUserHandleService;
    private final UserHandleService<TobUserInfo> tobUserInfoUserHandleService;
    private final UserHandleService<SysUserInfo> sysUserInfoUserHandleService;
    private final UserHandleService<DevUserInfo> devUserInfoUserHandleService;

    public UserServiceContextWrapper(UserServiceContext userServiceContext,
                                     @Qualifier("tocUserServiceImpl") UserHandleService<TocUserInfo> tocUserInfoUserHandleService,
                                     @Qualifier("tobUserServiceImpl") UserHandleService<TobUserInfo> tobUserInfoUserHandleService,
                                     @Qualifier("systemUserServiceImpl") UserHandleService<SysUserInfo> sysUserInfoUserHandleService,
                                     @Qualifier("developerUserServiceImpl") UserHandleService<DevUserInfo> devUserInfoUserHandleService) {
        this.userServiceContext = userServiceContext;
        this.tocUserInfoUserHandleService = tocUserInfoUserHandleService;
        this.tobUserInfoUserHandleService = tobUserInfoUserHandleService;
        this.sysUserInfoUserHandleService = sysUserInfoUserHandleService;
        this.devUserInfoUserHandleService = devUserInfoUserHandleService;
    }

    // 初始化一次
    @PostConstruct
    public void init() {
        userServiceContext.setHandlers(tocUserInfoUserHandleService);
        userServiceContext.setHandlers(tobUserInfoUserHandleService);
        userServiceContext.setHandlers(sysUserInfoUserHandleService);
        userServiceContext.setHandlers(devUserInfoUserHandleService);
    }

    public UserHandleService<Object> getUserService(String type) {
        return userServiceContext.getHandle(type);
    }
}
